package com.neusoft.EYSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.EYSystem.dao.CheckoutApplyDAO;
import com.neusoft.EYSystem.po.*;
import com.neusoft.EYSystem.service.CheckoutApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/checkoutApply")
public class CheckoutApplyController {
    @Autowired
    private CheckoutApplyService checkoutApplyService;

    @Autowired
    private CheckoutApplyDAO checkoutApplyDAO;

    @GetMapping("/page")
    public PageResponseBean<List<ClientCheckoutApply>> checkoutPage(String name, String checkoutDateStart, String checkoutDateEnd, String approvalStatus, String userId, Long page, Long size) {
        return checkoutApplyService.checkoutPage(name, checkoutDateStart, checkoutDateEnd, approvalStatus, userId, page, size);
    }

    @GetMapping("/listNum")
    public ResponseBean<Map<String, Integer>> getCheckoutApplyNum() {
        ResponseBean<Map<String, Integer>> response = new ResponseBean<>();
        try {
            Map<String, Integer> count = new HashMap<>();
            // 查询一周内每天的数量
            for (int i = 0; i < 7; i++) {
                String dateKey = java.time.LocalDate.now().minusDays(i).toString();
                QueryWrapper<CheckoutApply> qw = new QueryWrapper<>();
                qw.eq("is_visible", true);
                qw.eq("approval_status", "通过");
                qw.eq("approval_time", dateKey);
                int num = checkoutApplyDAO.selectCount(qw).intValue();
                count.put(dateKey, num);
            }
            response.setStatus(200);
            response.setMsg("记录数量查询成功");
            response.setData(count);
        } catch (Exception e) {
            response.setStatus(500);
            response.setMsg("记录数量查询失败: " + e.getMessage());
        }
        return response;
    }



    @GetMapping("/getCheckoutNumById")
    public ResponseBean<Map<String, Integer>> getNum(Integer userId) {
        ResponseBean<Map<String, Integer>> response = new ResponseBean<>();

        return response;
    }

    @GetMapping("/getCheckoutApply")
    public ResponseBean<CheckoutApply> getCheckoutApply(Integer applyId) {
        ResponseBean<CheckoutApply> response = new ResponseBean<>();
        try {
            CheckoutApply checkoutApply = checkoutApplyDAO.selectById(applyId);
            if (checkoutApply != null) {
                response.setStatus(200);
                response.setMsg("退房申请查询成功");
                response.setData(checkoutApply);
            } else {
                response.setStatus(404);
                response.setMsg("退房申请不存在");
            }
        } catch (Exception e) {
            response.setStatus(500);
            response.setMsg("退房申请查询失败: " + e.getMessage());
        }
        return response;
    }

    @PostMapping("/approval")
    public ResponseBean<Integer> approvalCheckoutApply(CheckoutApply checkoutApply) {
        return checkoutApplyService.approvalCheckoutApply(checkoutApply);
    }

    @PostMapping("/apply")
    public ResponseBean<Integer> applyCheckout(CheckoutApply checkoutApply) {
        ResponseBean<Integer> response = new ResponseBean<>();
        checkoutApply.setVisible(true);
        try {
            int result = checkoutApplyDAO.insert(checkoutApply);
            if (result > 0) {
                response.setStatus(200);
                response.setMsg("退房申请提交成功");
                response.setData(result);
            } else {
                response.setStatus(400);
                response.setMsg("退房申请提交失败，可能是数据不完整或重复");
            }
        } catch (Exception e) {
            response.setStatus(500);
            response.setMsg("退房申请提交失败: " + e.getMessage());
        }
        return response;
    }

    @PostMapping("/delete")
    public ResponseBean<Integer> deleteCheckoutApply(Integer applyId) {
        return checkoutApplyService.deleteLogic(applyId);
    }

    @PostMapping("/deleteLogic")
    public ResponseBean<Integer> deleteLogic(Integer applyId) {
        return checkoutApplyService.deleteLogic(applyId);
    }

    @PostMapping("/update")
    public ResponseBean<String> updateCheckoutApply(CheckoutApply checkoutApply) {
        ResponseBean<String> response = new ResponseBean<>();
        try {
            int result = checkoutApplyDAO.updateById(checkoutApply);
            if (result > 0) {
                response.setStatus(200);
                response.setMsg("退房申请更新成功");
            } else {
                response.setStatus(404);
                response.setMsg("退房申请未找到或未做任何更改");
            }
        } catch (Exception e) {
            response.setStatus(500);
            response.setMsg("退房申请更新失败: " + e.getMessage());
        }
        return response;
    }

}
